<?php
class Newscategory_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    /*
	查询所有分类
    */
    function GetCategoryAllList()
    {
    	$this->load->database();
    	$sql = "SELECT * FROM newscategory"; 

		$query = $this->db->query($sql);

		if ($query->num_rows() > 0)
		{
		   return $query->result_array(); 		 
		}
		
    }

    /**
    * 获取分类树结构
    */
    function GetCategoryTree()
    {
        $list = array();
        $templist = $this->GetCategoryListByParent(0);
        $qz = "├";//前缀
        foreach ($templist as $item) {
            $listindex = count($list);
            $item['Sort']= $listindex;
            $list[$listindex] = $item;
            $this->GetCategoryListByParentId($item['CategoryId'],$qz,$list);
        }
        return $list;
        //print_r($list);
    }

    /**
    * 获取分类树结构
    */
    function GetCategoryListByParentId($parentId,$qz,&$list)
    {
        $temp = $this->GetCategoryListByParent($parentId);
        if(count($temp)>0) {
            $qz = $qz."─ ";
        }
        if(is_array($temp))    
        {                            
            foreach ($temp as $item) {
                $listindex = count($list);
                $item['Sort']= ($listindex + 1);
                $item['CategoryName'] = $qz . $item['CategoryName'];
                $list[$listindex] = $item;
            }
        }
    }

    /*
    查询指定父类下面的子类
    $parentId 父类ID（int）
    */
    function GetCategoryListByParent($parentId)
    {
        $this->load->database();
        $sql = "SELECT * FROM newscategory WHERE ParentId = ?;"; 


        $query = $this->db->query($sql, array($parentId));

        if ($query->num_rows() > 0)
        {
           return $query->result_array();         
        }
    }


    /**
    * 获取实体
    */
    function GetCategoryEntity($id)
    {
        $this->load->database();
        $sql = "SELECT * FROM newscategory WHERE CategoryId = ? LIMIT 1;"; 


        $query = $this->db->query($sql, array($id));

        if ($query->num_rows() > 0)
        {
           return $query->row();         
        }
    }

    /**
    * 添加分类
    */
    function AddCategory($name,$description,$parentid)
    {
        $data = array('CategoryName' => $name,'ParentId' => $parentid,'Comment' => $description,'CreateOn' => 'NOW()','CreateBy' => 'system','ModifyOn' => 'NOW()' );
        //$sql = "INSERT INTO newscategory (CategoryName,ParentId,Comment,CreateOn,CreateBy,ModifyOn) VALUES (?,?,?,NOW(),?,NOW());";
        $this->load->database();
        $result = $this->db->insert('newscategory',$data);
        return $result;
    }

    /**
    * 修改分类
    */
    function EditCategory($id,$name,$description,$parentid)
    {
        $data = array('CategoryName' => $name,'ParentId' => $parentid,'Comment' => $description,'ModifyBy' => 'system','ModifyOn' => 'NOW()' );
        $where = array('CategoryId' => $id );

        $this->load->database();
        $result = $this->db->update('newscategory',$data,$where);
        return $result;
    }

    /**
    * 删除
    */
    function DeleteCategory($id)
    {
        $where = array('CategoryId' => $id );
        $this->load->database();
        $result = $this->db->delete('newscategory',$where);
        return $result;
    }
}

?>